/**
 * class nwsim
 */

package nwsim;

public class NwSim {
	
	private static Node[] nodeArray;
	private static Channel channel;
	public static int ETX = 0;
	static int killCount = 0;

	public static void main(String[] args) {
		channel = new Channel();
		initNodes();
		discoverNeighbors();
		System.out.println("Starting simulation...");
		startSimulation();
	}
	
	static void initNodes() {
		nodeArray = new Node[Constants.nodeCount];
		for(int i=0; i<Constants.nodeCount; i++) {
			nodeArray[i] = new Node(i, getCoordinates(i));
		}
	}
	
	static Coordinates getCoordinates(int nodeID) {
		Coordinates pos = new Coordinates();
		
		//for 3 node network for network coding
		switch(nodeID) {
			
			case 0: pos.x = 0; 		pos.y = 0; 		break;
			case 1: pos.x = 500; 	pos.y = 0; 		break;
			case 2: pos.x = 1000; 	pos.y = 0; 		break;
			default: System.out.println("What the hell!!!"); break;
		}
		
		return pos;
	}
	
	static void discoverNeighbors() {
		nodeArray[0].addNeighbor(1, 1);
		nodeArray[1].addNeighbor(0, 1);
		nodeArray[1].addNeighbor(2, 1);
		nodeArray[2].addNeighbor(1, 1);
	}
	
	public static Channel getChannel() {
		return channel;
	}
	
	static void startSimulation() {
		for(int i=0; i<Constants.nodeCount; i++) {
			nodeArray[i].start();
		}
	}
	
	public static void stopSimulation() {
		killCount++;
		if(killCount == 2) {
			for(int i=0; i<Constants.nodeCount; i++) {
				nodeArray[i].stopThread = true;
				nodeArray[i].interrupt();
			}
			channel.cancel();
			System.out.println("ETX = " + NwSim.ETX);
			System.exit(0);
		}
	}
}
